﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace LightGameLibs
{
    public class DelayTimer
    {

        private static MBSingleton s_mono;

        private static DelayTimer s_inst;

        public static DelayTimer Inst
        {
            get
            {
                if (null == s_inst)
                {
                    s_inst = new DelayTimer();
                }

                if (null == s_mono)
                {
                    MBSingleton.GetMBSingleton();
                }
                return s_inst;
            }
        }

        private Dictionary<object, Dictionary<string, Coroutine>> m_routineDict = new Dictionary<object, Dictionary<string, Coroutine>>();

        public void Invoke(object refer, string tag, IEnumerator routine, float time)
        {
            if (null == s_mono)
            {
                return;
            }

            Dictionary<string, Coroutine> coroDict = null;
            if (!m_routineDict.TryGetValue(refer, out coroDict))
            {
                DebugMgr.Log(DebugTag.UTILITY, "Reference not exist, Try to add");
                var tempRout = s_mono.StartCoroutine(DelayStart(routine, time));
                var collector = new Dictionary<string, Coroutine>();
                collector.Add(tag, tempRout);
                m_routineDict.Add(refer, collector);
                return;
            }

            if (!coroDict.ContainsKey(tag))
            {
                DebugMgr.Log(DebugTag.UTILITY, "Tag not exist, Try to add");
                var tempRout = s_mono.StartCoroutine(DelayStart(routine, time));
                coroDict.Add(tag, tempRout);
                return;
            }

            Stop(refer, tag);
            Invoke(refer, tag, routine, time);
        }

        private IEnumerator DelayStart(IEnumerator routine, float time)
        {
            yield return new WaitForSeconds(time);

            if (null == s_mono)
            {
                yield break;
            }

            s_mono.StartCoroutine(routine);
        }

        public void Stop(object refer, string tag)
        {
            if (null == s_mono)
            {
                return;
            }

            Dictionary<string, Coroutine> coroDict = null;
            if (!m_routineDict.TryGetValue(refer, out coroDict))
            {
                DebugMgr.Log(DebugTag.UTILITY, "Object not found");
                return;
            }

            Coroutine coro = null;
            if (!coroDict.TryGetValue(tag, out coro))
            {
                DebugMgr.Log(DebugTag.UTILITY, "Tag not found");
                return;
            }

            s_mono.StopCoroutine(coro);
        }

        public void StopAll(object refer)
        {
            if (null == s_mono)
            {
                return;
            }

            Dictionary<string, Coroutine> coroDict = null;
            if (!m_routineDict.TryGetValue(refer, out coroDict))
            {
                DebugMgr.Log(DebugTag.UTILITY, "Object not found");
                return;
            }

            foreach (var item in coroDict)
            {
                s_mono.StopCoroutine(item.Value);
            }
        }
    }
}
